import { timeFormat } from '../../model/utils/common';

export default {
    namespace: 'consumption',
    state: {
        totalPrice: 0,
        carNumber: '',
        carList: [],
        list: [],
    },
    reducers: {
        save(state, { payload }) {
            return Object.assign({}, state, payload);
        }
    },
    effects: {
        * getCarList({ payload }, { put, call, select }){
        // 查询汽车列表
            const carList = yield put.resolve({
                type : '$request/get',
                payload : {
                    url : '/api/getCarList'
                }
            });

            let price = {};
            if(carList && carList.length){
                const carNumber = wx.getStorageSync('carNumber');
                let car = carList.find(item => item.carNumber == carNumber) || carList[0];
                wx.setStorageSync('carNumber', car.carNumber);
                yield put({
                    type : 'save',
                    payload : {
                        carList,
                        price,
                        carNumber: car.carNumber
                    }
                });
                // 第一次初始化
                if(!payload.carNumber){
                    yield put({
                        type: 'init',
                        payload: {
                            carNumber : car.carNumber
                        }
                    });
                }
            }else{
                yield put({
                    type: 'init',
                    payload: {
                        carNumber : ''
                    }
                });
            }
            return carList;
        },
        *init({ payload }, { put, call, select }){

            const {carNumber} = payload;
            const result = yield put.resolve({
                type : '$request/post',
                payload : {
                    url : '/consumption/list',
                    data: {carNumber}
                }
            });
            // 如果没数据，直接到创建页面
            if(!result || !result.length){
                return wx.redirectTo({
                    url: '/pages/record/index',
                });
            }

            const recordMap  = {};
            const typeList = ['交停车费','交过路费','交保险费','交违章罚款','汽车维修费','购买汽车配饰','交充电费','其他费用'];
            result.forEach(item => {
                item.price = item.price/100;
                item.timeStr = timeFormat(item.time, 'YYYY-MM-DD hh:mm:ss');
                item.recordType = typeList[item.recordType];
            });
            let totalPrice = 0;
            result.forEach(item => {
                const month = timeFormat(item.time, 'YYYY年MM月');
                if(recordMap[month]){
                    recordMap[month].push(item);
                }else{
                    recordMap[month] = [item];
                }
                totalPrice += item.price;
            });
            const list = Object.keys(recordMap).map(key => {
                let total = 0;
                recordMap[key].forEach(item => total += item.price);
                return {
                    month: key,
                    total,
                    list : recordMap[key]
                };
            });

            yield put({
                type: 'save',
                payload: {
                    list,
                    totalPrice,
                    carNumber
                }
            });


        },

    }
};